import { login, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import { Message } from 'element-ui'
import Cookies from 'js-cookie'

const getDefaultState = () => {
  return {
    token: getToken(),
    uid: '',
    roles: [],
    name: '',
    dept: '',
    deptName: '',
    account: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_UID: (state, uid) => {
    state.uid = uid
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_Dept: (state, dept) => {
    state.dept = dept
  },
  SET_DeptName: (state, deptName) => {
    state.deptName = deptName
  },
  SET_Account: (state, account) => {
    state.account = account
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
        Message.success('登陆成功')
      }).catch(error => {
        reject(error)
      })
    })
  },

  // SSO login
  loginBySSO({ commit }, token) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', token)
      setToken(token)
      resolve()
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { uid, roles, name, dept, account, deptName } = data
        commit('SET_UID', uid)
        commit('SET_ROLES', roles)
        commit('SET_NAME', name)
        commit('SET_Dept', dept)
        commit('SET_DeptName', deptName)
        commit('SET_Account', account)
        resolve(data)
        Message.success('获取用户信息成功')
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // 清除所有cookie以确保完全退出
      Object.keys(Cookies.get()).forEach(cookieName => {
        Cookies.remove(cookieName)
      })
      removeToken() // must remove token first
      resetRouter()
      commit('RESET_STATE')
      commit('SET_ROLES', [])
      Message.success('退出登录成功')
      resolve()
      const url = 'http://218.197.18.189:9528/sso/logout'
      const link = document.createElement('a')
      link.href = url // 设置跳转目标地址
      link.rel = 'noreferrer' // 隐藏跳转来源信息
      link.click() // 模拟点击触发跳转
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

